package com.sxfq.loansupermarket.backsystem.modular.basicData.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.RefereeShipInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchRefereeShipInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IRefereeShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 性格管理
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/refereeShip")
public class RefereeShipController extends BaseController {

    @Autowired
    IRefereeShipService service;

    private String PREFIX = "/basicData/refereeShip/";

    /**
     * 跳转到性格管理页面
     */
    @RequestMapping("")
    public String refereeShip(Model model) {
        return PREFIX + "/refereeShip.html";
    }

    /**
     * 跳转到预约客户管理添加页面
     */
    @RequestMapping(value = "/refereeShip_add")
    public String refereeShipAdd() {
        return PREFIX + "refereeShip_add.html";
    }


    /**
     * 跳转到性格管理详情页面
     */
    @RequestMapping(value = "/refereeShip_update/{refereeShipId}")
    public String refereeShipUpdate(@PathVariable String refereeShipId, Model model) {
        if (ToolUtil.isEmpty(refereeShipId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        RefereeShipInfo refereeShipInfo = this.service.selectOneById(refereeShipId);
        model.addAttribute("refereeShipInfo", refereeShipInfo);
        return PREFIX + "refereeShip_edit.html";
    }

    /**
     * 性格管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchRefereeShipInfo info, HttpServletRequest request) {
        Page<RefereeShipInfo> page = new PageFactory<RefereeShipInfo>().defaultPage();
        try {
            List<RefereeShipInfo> list = service.selectPageListByQueryGetMap(page, info);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 添加性格
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public int add(@Valid RefereeShipInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.insert(info);
    }


    /**
     * 删除预约客户
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public Tip delete(@PathVariable String id) throws Exception {
        service.delete(id);
        return SUCCESS_TIP;
    }

    /**
     * 修改考题
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public int update(@Valid RefereeShipInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.update(info);
    }


    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public  List<Map<String,Object>> getList() {
        List<Map<String,Object>> list= service.list();
        return list;
    }
}
